//index.js
Page({
  data:{
    item:0,
    tab:0,
    playlist: [{
      id: 1,
      title: 'thesapientdream',
      singer: 'sapientdream',
      src: 'http://music.163.com/song/media/outer/url?id=1325711844.mp3',
      coverImgUrl: 'http://p1.music.126.net/KJFf4uegabgAKC5TF86gZQ==/109951163689697511.jpg'
    }, {
      id: 2,
      title: '模特',
      singer: '李荣浩',
      src: 'http://music.163.com/song/media/outer/url?id=27731176.mp3',
      coverImgUrl: 'http://p2.music.126.net/0uZ_bKtm4E188Uk9LFN1qg==/109951163187393370.jpg'
    }, {
      id: 3,
      title: '溯',
      singer: '马吟吟',
      src: 'https://link.hhtjim.com/qq/002eDlpu4U223F.mp3',
      coverImgUrl: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000002yOiJ347ly2o_1.jpg'
    }, {
      id: 4,
      title: '嚣张',
      singer: 'en',
      src: 'http://music.163.com/song/media/outer/url?id=1382596189.mp3',
      coverImgUrl: 'http://p2.music.126.net/NhkuFBphLFaSmYMeW1-frQ==/109951164271814514.jpg'
    },{
      id: 5,
      title: '欧若拉',
      singer: '张韶涵',
      src: 'http://music.163.com/song/media/outer/url?id=36841951.mp3',
      coverImgUrl: 'http://p1.music.126.net/_8EWgYcYrfZrv1fCgO-Mbw==/3405187513733087.jpg'
    },{
      id: 6,
      title: 'Sugar',
      singer: 'Maroon 5',
      src: 'http://music.163.com/song/media/outer/url?id=29019227.mp3',
      coverImgUrl: 'http://p2.music.126.net/RkbAVgGxk1Nr5fjuaR9dww==/19175482788569403.jpg'
    },{
      id: 7,
      title: 'Love Story',
      singer: 'Taylor Swift',
      src: 'http://music.163.com/song/media/outer/url?id=1457707546.mp3',
      coverImgUrl: 'http://p1.music.126.net/JoVareIX82eEzbrYtJLAPA==/109951165084884378.jpg'
    },{
      id: 8,
      title: 'Welcome To New York',
      singer: 'Taylor Swift',
      src: 'http://music.163.com/song/media/outer/url?id=1457707551.mp3',
      coverImgUrl: 'http://p2.music.126.net/KW6XyA88AXChVJo2Yu-sAw==/109951165163645918.jpg'
    },{
      id: 9,
      title: 'Let Me Love You',
      singer: 'DJ Snake / Justin Bieber',
      src: 'http://music.163.com/song/media/outer/url?id=420922950.mp3',
      coverImgUrl: 'http://p2.music.126.net/1wuBM5YSaQGd6ua9_Eac7w==/3265549562091723.jpg'
    },{
      id: 10,
      title: '她说',
      singer: '林俊杰',
      src: 'http://music.163.com/song/media/outer/url?id=25638767.mp3',
      coverImgUrl: 'http://p2.music.126.net/dI9UCcU9Qx_qLdowsc2z2w==/1406275388402888.jpg'
    },{
      id: 11,
      title: '大鱼',
      singer: '周深',
      src: 'http://music.163.com/song/media/outer/url?id=413812448.mp3',
      coverImgUrl: 'http://p1.music.126.net/aiPQXP8mdLovQSrKsM3hMQ==/1416170985079958.jpg'
    },{
      id: 12,
      title: 'But U',
      singer: 'NINEONE#',
      src: 'http://music.163.com/song/media/outer/url?id=1459232593.mp3',
      coverImgUrl: 'http://p1.music.126.net/li19i75jz6GGOT79IyAjYA==/109951165100592039.jpg'
    },{
      id: 13,
      title: '我的名字',
      singer: '焦迈奇',
      src: 'http://music.163.com/song/media/outer/url?id=554241732.mp3',
      coverImgUrl: 'http://p2.music.126.net/a_rb8pgC5R_hO-lfm7acSw==/109951164019567772.jpg'
    },{
      id: 14,
      title: '说谎',
      singer: '林宥嘉',
      src: 'http://music.163.com/song/media/outer/url?id=108390.mp3',
      coverImgUrl: 'http://p1.music.126.net/mMZNB-jhYsw29K61QtopJA==/109951163187404137.jpg'
    }, ],
    state: 'paused',
    playIndex: 0,
    play: {
      currentTime: '00:00',
      duration: '00:00',
      percent: 0,
      title: '',
      singer: '',
      coverImgUrl: '/images/cover.jpg',
    },
    

  },
  audioCtx: null,
    onReady: function(){
      this.audioCtx = wx.createInnerAudioContext()
      //默认选择第1曲
      this.setMusic(0)
    },
    setMusic:function(index){
      var music = this.data.playlist[index]
      this.audioCtx.src = music.src
      this.setData({
        playIndex: index,
        'play.title': music.title,
        'play.singer': music.singer,
        'play.coverImgUrl': music.coverImgUrl,
        'play.currentTime': '00:00',
        'play.durration': '00:00',
        'play.percent': 0
      })
    },
       // 实现播放器播放功能
  audioCtx: null,
  onReady: function() {
    this.audioCtx = wx.createInnerAudioContext()
    // 默认选择第1曲
    this.setMusic(0)
    var that = this
    // 播放进度检测
    this.audioCtx.onError(function() {
      console.log('播放失败：' + that.audioCtx.src)
    })
    // 播放完成自动换下一曲
    this.audioCtx.onEnded(function() {
      that.next()
    })
    // 自动更新播放进度
    this.audioCtx.onPlay(function() {})
    this.audioCtx.onTimeUpdate(function() {
      that.setData({
        'play.duration': formatTime(that.audioCtx.duration),
        'play.currentTime': formatTime(that.audioCtx.currentTime),
        'play.percent': that.audioCtx.currentTime / that.audioCtx.duration * 100
      })
    })
    // 格式化时间
    function formatTime(time) {
      var minute = Math.floor(time / 60) % 60;
      var second = Math.floor(time) % 60
      return (minute < 10 ? '0' + minute : minute) + ':' + (second < 10 ? '0' + second : second)
    }
  },

  // 播放按钮
  play: function() {
    this.audioCtx.play()
    this.setData({
      state: 'running'
    })
  },

  // 暂停按钮
  pause: function() {
    this.audioCtx.pause()
    this.setData({
      state: 'paused'
    })
  },

    // 下一曲按钮
    next: function() {
      var index = this.data.playIndex >= this.data.playlist.length - 1 ? 0 : this.data.playIndex + 1
      this.setMusic(index)
      if (this.data.state === 'running') {
        this.play()
      }
    },

  
  changeItem:function(e){
    this.setData({
      item: e.target.dataset.item
    })
  },
  changeTab:function(e){
    this.setData({
      tab: e.detail.current
    })
  },

    // 滚动条调节歌曲进度
    sliderChange: function(e) {
      var second = e.detail.value * this.audioCtx.duration / 100
      this.audioCtx.seek(second)
    },

    // 播放列表换曲功能
    change: function(e) {
    this.setMusic(e.currentTarget.dataset.index)
    this.play()
  }

})

